package com.vogue.supplymall.order.domain.dao.impl;

import javax.annotation.Resource;

import com.vogue.supplymall.common.domain.dto.CommentInfo;
import com.vogue.supplymall.common.domain.dto.RelevanceInfo;
import org.springframework.stereotype.Repository;

import com.vogue.supplymall.order.domain.dao.CommentDao;
import com.vogue.supplymall.order.domain.mapper.CommentMapper;
import com.vogue.supplymall.order.domain.model.Comment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("commentDao")
public class CommentDaoImpl extends BaseDaoImpl<Comment, Long> implements CommentDao {

    @Resource
    private CommentMapper commentMapper;

    @Override
    public Map<String, Object> getCommentingCount(RelevanceInfo relevanceInfo) {
        Map<String, Object> map = new HashMap<>();
        map.put("userid", relevanceInfo.getUserId());
        map.put("syscodeid", relevanceInfo.getSyscodeid());
        map.put("organid", relevanceInfo.getOrganid());
        String sql = "select COUNT(p.id) commenting_count from order_item_product p,`order` o,order_buyer b,order_status ost "
                + "where b.userid = #{userid} and b.syscodeid = #{syscodeid} and b.organid = #{organid} "
                + "and p.commentstate = 0 and p.disabledflag = 0 and b.orderid = p.orderid "
                + "AND o.id = p.orderid AND ost.orderstatus = 4 AND ost.orderid = o.id "
                + "AND o.createdate >= date_sub(curdate(),interval 1 year) and o.createdate <= NOW() ";
        List<Map<String, Object>> list = this.query0(sql, map);
        if (list == null) {
            map = new HashMap<String, Object>();
            map.put("commenting_count", 0);
            return map;
        } else {
            return list.get(0);
        }
    }

    @Override
    public Map<String, Object> getCommentingCount(CommentInfo relevanceInfo) {
        Map<String, Object> map = new HashMap<>();
        map.put("userid", relevanceInfo.getLoginId());
        map.put("syscodeid", relevanceInfo.getSyscodeid());
        map.put("organid", relevanceInfo.getOrganid());
        String sql = "select COUNT(p.id) commenting_count from order_item_product p,`order` o,order_buyer b,order_status ost "
                + "where b.userid = #{userid} and b.syscodeid = #{syscodeid} and b.organid = #{organid} "
                + "and p.commentstate = 0 and p.disabledflag = 0 and b.orderid = p.orderid "
                + "AND o.id = p.orderid AND ost.orderstatus = 4 AND ost.orderid = o.id "
                + "AND o.createdate >= date_sub(curdate(),interval 1 year) and o.createdate <= NOW() ";
        List<Map<String, Object>> list = this.query0(sql, map);
        if (list == null) {
            map = new HashMap<String, Object>();
            map.put("commenting_count", 0);
            return map;
        } else {
            return list.get(0);
        }
    }

    @Override
    public Map<String, Object> getFinishCommentCount(RelevanceInfo relevanceInfo) {
        Map<String, Object> map = new HashMap<>();
        map.put("userid", relevanceInfo.getUserId());
        map.put("syscodeid", relevanceInfo.getSyscodeid());
        map.put("organid", relevanceInfo.getOrganid());

        String sql = "select COUNT(c.id) fishComment_count "
                + "from `comment` c ,order_buyer ob "
                + "WHERE c.orderid = ob.orderid "
                + "and c.buyerid = #{userid} and ob.syscodeid = #{syscodeid} and ob.organid = #{organid} "
                + "AND c.orderdate >= date_sub(curdate(),interval 1 year) and c.orderdate <= NOW() "
                ;

        List<Map<String, Object>> list = this.query0(sql, map);
        if (list == null) {
            map = new HashMap<String, Object>();
            map.put("fishComment_count", 0);
            return map;
        } else {
            return list.get(0);
        }
    }

    @Override
    public List<Map<String, Object>> findBuyerCommentList(CommentInfo commentInfo) {
        return commentMapper.findBuyerCommentList(commentInfo);
    }

    @Override
    public List<Map<String, Object>> findBuyerCommentDetail(CommentInfo commentInfo) {
        Map<String, Object> map = new HashMap<>();
        map.put("commentid", commentInfo.getCommentid());

        String sql = " SELECT c.id commentid,d.id commentdetailid,c.commentmark,d.commenttext,d.modifydate,"
                + "c.shopid,c.shopname,selld.commenttext sellcommenttext,selld.modifydate sellmodifydate,c.orderid  "
                + "FROM comment_detail d,`comment` c "
                + "left JOIN comment_detail selld  ON selld.commentid = c.id and selld.commentobejct = 1 "
                + "WHERE c.id = d.commentid and d.commentobejct =0 "
                + "and c.id = #{commentid} "
                + "order by d.id desc ";

        List<Map<String, Object>> list = this.query0(sql,map);
        return list;
    }

    @Override
    public List getProductImgUrls(String commentid, String commentdetailid) {

        Map<String, Object> map = new HashMap<>();
        map.put("commentid", commentid);
        map.put("commentdetailid", commentdetailid);

        String sql = "SELECT type,url from comment_img i "
                + "where i.commentid = #{commentid} and i.commentdetailid = #{commentdetailid} "
                + "order by imgsort ";

        List<Map<String, Object>> list = this.query0(sql,map);
        return list;
    }

    @Override
    public List<Map<String, Object>> findSellerCommentList(CommentInfo commentInfo) {
        return commentMapper.findSellerCommentList(commentInfo);
    }

    @Override
    public List<Map<String, Object>> findSellerCommentDetail(CommentInfo commentInfo) {
        Map<String, Object> map = new HashMap<>();
        map.put("commentid", commentInfo.getCommentid());

        String sql = " SELECT c.id commentid,d.id commentdetailid,c.commentmark,d.commenttext,d.modifydate,"
                + "c.shopid,c.shopname,selld.id sellid, selld.commenttext sellcommenttext,selld.modifydate sellmodifydate,c.orderid,c.burersyscodeid,c.buyerid "
                + "FROM comment_detail d,`comment` c "
                + "left JOIN comment_detail selld  ON selld.commentid = c.id and selld.commentobejct = 1 "
                + "WHERE c.id = d.commentid and d.commentobejct =0 "
                + "and c.id = #{commentid} "
                + "order by d.id desc ";

        List<Map<String, Object>> list = this.query0(sql,map);
        return list;
    }
    
    @Override
	public String delSellerComment() {
		String sql="delete from comment_detail where id=#{detailId}";
		
		return sql;
	}

	@Override
	public String setValueForAnswerFlag() {
		String sql="update comment com,comment_detail cd "
				+ " set com.answerflag=#{answerFlag},cd.answerflag=#{answerFlag}"
				+ " where com.id=#{commentId} and com.id=cd.commentid";
		
		return sql;
	}

	@Override
	public String setValueForReadFlag() {
		String sql="update comment_detail cd"
				+ " set cd.readflag=1"
				+ " where cd.commentid=#{commentId} and cd.commentobejct=#{objectFlag}";
		return sql;
	}

	@Override
	public String updateRestoreflag() {
		String sql="update comment set restoreflag=#{restoreFlag}"
				+ " where id=#{commentId}";
		
		return sql;
	}

    @Override
    public List<Map<String, Object>> getDefaultAnswer(CommentInfo commentInfo) {
        Map<String, Object> map = new HashMap<>();
        map.put("shopid", commentInfo.getShopid());
        map.put("syscodeid", commentInfo.getSyscodeid());

        String sql = "SELECT answertext FROM comment_defaultanswer "
                + "where shopid =  #{shopid} and sellersyscodeid =  #{syscodeid}";

        List<Map<String, Object>> list = this.query0(sql,map);
        return list;
    }

}
